#/*===========================================================================
# Replication code for: 
# "Using Violence, Seeking Votes: Introducing the Militant Group Electoral 
# Participation (MGEP) Dataset"
# Aila M. Matanock
#---------------------------------------------------------------------------
#  Creation Date:      February 6, 2016
#  Modification Date:  June 20, 2016
#  Do-file version:    06
#  Output:             Graphs
#===========================================================================*/

#/*===============================================================================================
#                                   0: Program set up
#===============================================================================================*/
# NOTES: 

# 1. Users can access MGEP_S2016 and all of the documentation at 
# https://dataverse.harvard.edu/dataverse/matanock.
# 2. Run Stata code before running this R code. This code using files created using Stata code. 
# 3. In the following code, replace "~root" with corresponding directory path created when 
# running the Stata code. 

# Set corresponding directory path. Replace "~/Results" with directory path corresponding to 
# "results" in Stata code. 

setwd("~/Results")

#clear environment
rm(list=ls())

# Load libraries
library(foreign)
library(knitr)
library(xtable)
library(reshape2)
library(ggplot2)
library(readstata13)

#/*===============================================================================================
#                                   1: Graphs main text
#===============================================================================================*/

## Figure 1. New instances (counts): 

# Load data

conflict.data=read.dta13("~root/dynamic_conflict0.dta")

# Keeping from 1970 to 2010
conflict.data<-subset(conflict.data, conflict.data$year>=1970 & conflict.data$year<=2010)

means.long<-melt(conflict.data,id.vars="year")

fig_1 <- subset(means.long, (means.long$variable=='viopr' | means.long$variable=='peacepr' | means.long$variable=='winnerr'))
fig_1 <- fig_1[order(fig_1$year),] 

##Graph 1 (Stacked)

# labels for stacked graphs: 
library(plyr)

df_sorted <- arrange(fig_1, year, factor(variable)) 
head(df_sorted)

df_cumsum <- ddply(df_sorted, "year", transform, label_ypos=cumsum(value))
df_cumsum$label_ypos[df_cumsum$label_ypos==0]=NA
df_cumsum$value[df_cumsum$value==0]=NA 
head(df_cumsum)

p<-ggplot(df_cumsum, aes(x = year, y = value, fill = factor(variable))) + geom_bar(stat = "identity") +
  xlab("Years") + ylab("Count of new instances of militant or \nex-militant group electoral participation") + 
  theme_minimal() + scale_x_continuous(breaks=seq(1970,2010,1)) + 
  scale_y_continuous(breaks=seq(0,max(df_cumsum$label_ypos,na.rm=TRUE),1)) +
  theme(axis.text.x = element_text(angle = 90, size=8, vjust=0.5)) +
  scale_fill_manual(name="Type of participation",
                    breaks=c("viopr", "peacepr", "winnerr"),
                    labels=c("Violent", "Peaceful", "Won"),
                    values=c("#CCCCCC", "#999999", "#666666")) +
  theme(legend.position="top") + theme(panel.background = element_rect(fill = "white", colour = "white"), 
  panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"), 
  panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white")) +
  theme(axis.title.x = element_text(vjust=-0.35)) + 
  guides(fill = guide_legend(keywidth = 0.5, keyheight = 0.5)) + geom_text(aes(y=label_ypos, label=value), vjust=1.6, 
                                                                           color="white", size=3.5)

#Exporting graph 1
ggsave(filename = "Figure1.pdf", plot = p, width=10, height=6)
ggsave(filename = "Figure1.png", plot = p, width=10, height=6)

##Graph 2 (Not stacked): Not in main text

p<-ggplot(data=fig_1, aes(x=year,y=value,fill=factor(variable))) +
  geom_bar(width=1, stat="identity",position="dodge") +
  xlab("Years")+ylab("Count of New Instances of Militant or \nEx-Militant Group Electoral Participation") + 
  theme_minimal() + scale_x_continuous(breaks=seq(1970,2010,1)) + 
  scale_y_continuous(breaks=seq(0,max(fig_1$value,na.rm=TRUE),1)) +
  theme(axis.text.x = element_text(angle = 90, size=8, vjust=0.5)) +
  scale_fill_manual(name="Type of participation",
                    breaks=c("viopr", "peacepr", "winnerr"),
                    labels=c("Violent", "Peaceful", "Won"),
                    values=c("#CCCCCC", "#999999", "#666666")) +
  theme(legend.position="top") + theme(panel.background = element_rect(fill = "white", colour = "white"), 
                                       panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"), 
                                       panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white")) +
  theme(axis.title.x = element_text(vjust=-0.35)) + 
  guides(fill = guide_legend(keywidth = 0.5, keyheight = 0.5))

#Exporting graph 2
ggsave(filename = "Figure1b.pdf", plot = p, width=10, height=6)

#/*===============================================================================================
#                                   2: Graphs Appendix
#===============================================================================================*/

# APPENDIX A2.2

## (Counts) Group Electoral Participation over Time (%)

# Preparing data for the graphs 

#Load data: 

conflict.data=read.dta13("~root/dynamic_conflict1.dta")
conflict.data<-subset(conflict.data, conflict.data$year>=1970 & conflict.data$year<=2010)

means.long<-melt(conflict.data,id.vars="year")
fig_2a <- subset(means.long, (means.long$variable=='viopr_a' | means.long$variable=='peacepr_a' 
                              | means.long$variable=='winnerr_a'))
fig_2a <- fig_2a[order(fig_2a$year),] 

#Graph 

# labels for stacked graphs: 

df_sorted <- arrange(fig_2a, year, factor(variable)) 
df_cumsum <- ddply(df_sorted, "year", transform, label_ypos=cumsum(value))
df_cumsum$label_ypos[df_cumsum$label_ypos==0]=NA
df_cumsum$value[df_cumsum$value==0]=NA 

p<-ggplot(df_cumsum, aes(x = year, y = value, fill = factor(variable))) + geom_bar(stat = "identity") +
  xlab("Years")+ylab("Count of Instances of Militant \nGroup Electoral Participation") + theme_minimal() + 
  scale_y_continuous(breaks=seq(0,max(df_cumsum$label_ypos,na.rm=TRUE),4)) +
  scale_x_continuous(breaks=seq(1970,2010,1)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))  +
  theme(legend.position="top") + 
  theme(panel.background = element_rect(fill = "white", colour = "white"), 
                                       panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"), 
                                       panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white")) +
  theme(axis.title.x = element_text(vjust=-0.35))  +
  guides(fill = guide_legend(keywidth = 0.5, keyheight = 0.5)) +
  scale_fill_manual(name="Type of participation",
                    breaks=c("viopr_a", "peacepr_a", "winnerr_a"),
                    labels=c("Violent", "Peaceful", "Won"),
                    values=c("#CCCCCC", "#999999", "#666666")) + geom_text(aes(y=label_ypos, label=value), vjust=1.6, 
                                                                         color="white", size=3.5)

#Exporting graph
ggsave(filename = "FigureA22apdf", plot = p, width=10, height=6)

# APPENDIX A2.2 (in text)

## (Percentage) Group Electoral Participation over Time (%) (group-years)

# Preparing data for the graphs 

#Load data: 

conflict.data=read.dta13("~root/dynamic_conflict1.dta")
conflict.data<-subset(conflict.data, conflict.data$year>=1970 & conflict.data$year<=2010)

means.long<-melt(conflict.data,id.vars="year")
fig_2b <- subset(means.long, (means.long$variable=='viopr_a_mgy' | means.long$variable=='peacepr_a_mgy' 
                              | means.long$variable=='winnerr_a_mgy'))
fig_2b <- fig_2b[order(fig_2b$year),] 

#Graph 

# labels for stacked graphs: 

df_sorted <- arrange(fig_2b, year, factor(variable)) 
df_cumsum <- ddply(df_sorted, "year", transform, label_ypos=cumsum(value))
df_cumsum$label_ypos[df_cumsum$label_ypos==0]=NA
df_cumsum$value[df_cumsum$value==0.0]=NA 
df_cumsum$value=round(df_cumsum$value,1)

p<-ggplot(df_cumsum, aes(x = year, y = value, fill = factor(variable))) + geom_bar(stat = "identity") +
  xlab("Years")+ylab("Percentage of Militant and Ex-Militant Group Years") + theme_minimal() + scale_x_continuous(breaks=seq(1970,2010,1)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_y_continuous(breaks=seq(0,max(df_cumsum$label_ypos,na.rm=TRUE),1)) +
  scale_fill_manual(name="Type of participation",
                    breaks=c("viopr_a_mgy", "peacepr_a_mgy", "winnerr_a_mgy"),
                    labels=c("Violent", "Peaceful", "Won"),
                    values=c("#CCCCCC", "#999999", "#666666")) +
  theme(legend.position="top") + theme(panel.background = element_rect(fill = "white", colour = "white"), 
                                       panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"), 
                                       panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white")) +
  theme(axis.title.x = element_text(vjust=-0.35)) + 
  guides(fill = guide_legend(keywidth = 0.5, keyheight = 0.5)) + geom_text(aes(y=label_ypos, label=value), vjust=1.6, 
                                                                           color="white", size=3)

#Exporting graph
ggsave(filename = "FigureA22.pdf", plot = p, width=10, height=6)

# APPENDIX A2.3 (in original text)

## (Percentage) Group Electoral Participation over Time (%) (group-election years)

# Preparing data for the graphs 

#Load data: 

conflict.data=read.dta13("~root/dynamic_conflict1.dta")
conflict.data<-subset(conflict.data, conflict.data$year>=1970 & conflict.data$year<=2010)

means.long<-melt(conflict.data,id.vars="year")
fig_3 <- subset(means.long, (means.long$variable=='viopr_a_mgey' | means.long$variable=='peacepr_a_mgey' 
                              | means.long$variable=='winnerr_a_mgey'))
fig_3 <- fig_3[order(fig_2b$year),] 

#Graph 

# labels for stacked graphs: 

df_sorted <- arrange(fig_3, year, factor(variable)) 
df_cumsum <- ddply(df_sorted, "year", transform, label_ypos=cumsum(value))
df_cumsum$label_ypos[df_cumsum$label_ypos==0]=NA
df_cumsum$value[df_cumsum$value==0.0]=NA 
df_cumsum$value=round(df_cumsum$value,1)

p<-ggplot(df_cumsum, aes(x = year, y = value, fill = factor(variable))) + geom_bar(stat = "identity") +
  xlab("Years")+ylab("Percentage of Militant and Ex-Militant Group Election Years") + theme_minimal() + 
  scale_y_continuous(breaks=seq(0,max(df_cumsum$label_ypos,na.rm=TRUE),2)) +
  scale_x_continuous(breaks=seq(1970,2010,1)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(name="Type of participation",
                    breaks=c("viopr_a_mgey", "peacepr_a_mgey", "winnerr_a_mgey"),
                    labels=c("Violent", "Peaceful", "Won"),
                    values=c("#CCCCCC", "#999999", "#666666")) +
  theme(legend.position="top") + theme(panel.background = element_rect(fill = "white", colour = "white"), 
                                       panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"), 
                                       panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white")) +
  theme(axis.title.x = element_text(vjust=-0.35)) + 
  guides(fill = guide_legend(keywidth = 0.5, keyheight = 0.5)) + 
  geom_text(aes(y=label_ypos, label=value), vjust=1.6, color="white", size=3)

#Exporting graph
ggsave(filename = "Figure23.pdf", plot = p, width=10, height=6)

